package com.learn.controller;

import com.learn.pojo.Result;
import com.learn.util.AliOSSUtil;
import com.learn.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import static org.jacoco.agent.rt.internal_43f5073.core.runtime.AgentOptions.OutputMode.file;

@Slf4j
@RestController
public class UploadController {

//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile img) {
//        log.info("username:{}, age:{}, img:{}", username, age, img);
//        String originalFilename = img.getOriginalFilename();
//        String realName = UUIDUtil.uuid() + (originalFilename != null ?
//                originalFilename.substring(originalFilename.lastIndexOf(".")) : null);
//
//        String path = "D:\\temp\\" + realName;
//        try {
//            img.transferTo(new File(path));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return new Result();
//    }
    @Autowired
    private AliOSSUtil aliOSSUtil;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("img:{}", image);
        //调用阿里云工具类
        String url = aliOSSUtil.getUrl(image);
        return Result.success(url);
    }
}
